﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class Users : BaseBAL<UserInfo, Models.User>
    {
        public static bool CheckUserExistByName(string userName)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.User.Where(w => w.Name == userName).Count() > 0;
            }
        }

        public static bool CheckUserExistByEmail(string email)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.User.Where(w => w.Email == email).Count() > 0;
            }
        }

        public static UserInfo GetUserInfoByName(string userName)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.User, bool>>> funcs = new List<Expression<Func<Models.User, bool>>>();
                funcs.Add(w => w.Name == userName);
                var dd = GetT2IQueryable(funcs, db);
                if (dd.Count() == 1)
                {
                    return TransT2ToT1(dd.Single());
                }
                else
                    return null;

            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        public static void DelInfo(int id)
        {
            UserExtend.DelByUserId(id);
            Delete(id);
        }

        /// <summary>
        /// 根据学校ID删除信息
        /// </summary>
        /// <param name="SchoolId"></param>
        public static void DelBySchoolId(int SchoolId)
        {
            IList<int> ids = UserExtend.GetUserIsBySchoolId(SchoolId);
            UserExtend.DelByUserId(ids);
            Delete(ids);
        }

        /// <summary>
        /// 以拼音缩写去的数据
        /// </summary>
        /// <param name="pinyin">拼音缩写</param>
        /// <param name="count">取几条</param>
        /// <returns></returns>
        public static IList<UserInfo> GetUserInfosByPinYin(string pinyin,int count)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                //return TransT2ToT1(GetT2IQueryable(t => t.PinYin.Contains(pinyin), db).ToList());
                if (count > 0)
                {
                    return db.GetTable<Models.User>().Where(t => t.PinYin.Contains(pinyin)).Take(count).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.User>().Where(t => t.PinYin.Contains(pinyin)).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }
        //通过email查询对象
        public static UserInfo GetUserInfoByEmail(string email)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.User, bool>>> funcs = new List<Expression<Func<Models.User, bool>>>();
                funcs.Add(w => w.Email == email);
                var dd = GetT2IQueryable(funcs, db);
                if (dd.Count() == 1)
                {
                    return TransT2ToT1(dd.Single());
                }
                else
                    return null;

            }
        }

        public static UserInfo GetUserInfoById(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.User, bool>>> funcs = new List<Expression<Func<Models.User, bool>>>();
                funcs.Add(w => w.Id == id);
                var dd = GetT2IQueryable(funcs, db);
                if (dd.Count() == 1)
                {
                    return TransT2ToT1(dd.Single());
                }
                else
                    return null;
            }
        }

        public static bool LoginUser(string email, string passWord, ref UserInfo userInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.User, bool>>> funcs = new List<Expression<Func<Models.User, bool>>>();
                funcs.Add(w => w.Email == email);
                funcs.Add(w => w.PassWord == passWord);
                var dd = GetT2IQueryable(funcs, db);
                if (dd.Count() > 0)
                {
                    userInfo = TransT2ToT1(dd.Single());
                    return true;
                }
                else
                    return false;
            }
        }

        public void Update(int id, string password)
        {

        }

        public static int GetUserID(string Username)
        {
            try
            {

                using (var db = DatabaseHelper.GetDashboardData())
                {
                    return db.User.Single(t => t.Name == Username).Id;
                }
            }
            catch
            {
                return 0;
            }
        }

        public static IList<int> GetUserIds(string RoleIds)
        {
            if (!PageValidate.IsEmpty(RoleIds))
            {
                using (var db = DatabaseHelper.GetDashboardData())
                {
                    return db.GetTable<Models.User>().Where(t => ArrayHelper.ConvertToIntArray(Module_String.splitstr(RoleIds, ",")).Contains(t.RoleId)).Select(t => t.Id).ToList();
                }
            }
            else
            {
                return new List<int>() { };
            }
        }

        public static IList<UserInfo> GetUserInfos(int first, int max)
        {
            using(var db=DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.User>().Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.User>().Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }
    }
}
